package com.rybbaby.tss.api.attendance;

import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.consts.RoleConsts;
import com.rybbaby.tss.core.hqlsearch.HqlGenerateUtil;
import com.rybbaby.tss.core.utils.AppResponse;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssClasses.service.ITssClassesService;
import com.rybbaby.tss.front.tssMealFee.entity.TssMealFee;
import com.rybbaby.tss.front.tssMealFee.service.ITssMealFeeService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.ViewMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.ITssMemberCheckAttendanceService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.IViewMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendanceHistory.service.ITssMemberCheckAttendanceHistoryService;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import com.rybbaby.tss.front.tssMemberPackageHistory.service.ITssMemberPackageHistoryService;
import com.rybbaby.tss.front.tssRightStatistic.entity.TssRightStatistic;
import com.rybbaby.tss.front.tssRightStatistic.service.ITssRightStatisticService;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import com.rybbaby.tss.platform.sysAdminUser.service.ISysAdminUserService;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author 雷春琼
 * @description 班级表管理
 * @createDate 2017-08-09 下午11:50:14
 */
@CrossOrigin
@RestController
@RequestMapping(value = "api/attendance", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class AttendanceApi extends BaseController {

    @Autowired
    private ISysAdminUserService sysAdminUserService;
    @Autowired
    private ITssMemberCheckAttendanceService tssMemberCheckAttendanceService;
    @Autowired
    private ITssClassesService tssClassesService;
    @Autowired
    private ITssMemberPackageService tssMemberPackageService;
    @Autowired
    private ITssMealFeeService tssMealFeeService;
    @Autowired
    private ITssRightStatisticService tssRightStatisticService;
    @Autowired
    private ITssMemberPackageHistoryService tssMemberPackageHistoryService;
    @Autowired
    private ITssMemberCheckAttendanceHistoryService tssMemberCheckAttendanceHistoryService;
    @Autowired
    private IViewMemberAttendClassRecordService viewMemberAttendClassRecordService;

    /**
     * 每日考勤
     * @param adminUserId
     * @param token
     * @return
     */
    @Token(validateSign = true)
    @RequestMapping(value = "dailyAttendance", method = RequestMethod.POST)
    public String dailyAttendance(String adminUserId, String token) {
        String resMsg = "查看每日考勤成功!";
        int resCode = 200;
        AppResponse response = new AppResponse();
        if(token == null && token.equals("")){
            resMsg = "token必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(adminUserId == null && "".equals(adminUserId)){
            resMsg = "用户id必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        try {

            //获取当前登录者
            String ids = "";
            SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
            //获取当前登录者所拥有角色
            List<String> roleList = super.getLoginAdminRoles(adminUserId);

            String adminUserIds = "";
            if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
                adminUserIds = adminUserId;

            } else{
                List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
                ids = StringUtils.join(idsList, "','");
                adminUserIds = ids;
            }

            //今日亲子/过渡考勤 班级列表
            List<Map<String , Object>> ClassAttendanceMap = tssMemberCheckAttendanceService.findClassAttendanceByType(adminUserIds);
            for (Map map : ClassAttendanceMap) {
                Integer sumRealAttendance =  Integer.parseInt(map.get("sumRealAttendance") == null ? "" : map.get("sumRealAttendance").toString());
                Integer sumShouldAttendAttendance =  Integer.parseInt(map.get("sumShouldAttendAttendance") == null ? "" : map.get("sumShouldAttendAttendance").toString());
                int ratio = 0;
                if(sumShouldAttendAttendance != 0) {
                    ratio = sumRealAttendance * 100 / sumShouldAttendAttendance;
                }
                map.put("ratio",ratio);
            }

            response.setData(ClassAttendanceMap);
        } catch (Exception e) {
            resCode = 500;
            resMsg = "查看每日考勤失败!";
            e.printStackTrace();
        }
        //return new ApiResponse(resMsg, resCode).jsonResponse(false);
        return response.jsonResponse(true);
    }
    /**
     * 考勤列表
     * @param adminUserId
     * @param token
     * @return
     */
    @Token(validateSign = true)
    @RequestMapping(value = "attendanceList", method = RequestMethod.POST)
    public String attendanceList(String adminUserId, PageBean paramPageBean,String notAttendance, String token) {
        String resMsg = "查看考勤列表成功!";
        int resCode = 200;
        AppResponse response = new AppResponse();
        if(token == null && token.equals("")){
            resMsg = "token必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(adminUserId == null && "".equals(adminUserId)){
            resMsg = "用户信息必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(notAttendance == null && "".equals(notAttendance)){
            resMsg = "考勤编码必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        try {
            //获取当前登录者
            String ids = "";
            SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
            //获取当前登录者所拥有角色
            List<String> roleList = super.getLoginAdminRoles(adminUserId);

            if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
                ids = adminUserId;
            } else{
                List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
                ids = StringUtils.join(idsList, "','");
            }


            PageBean pageBean = tssClassesService.attendancePageList(notAttendance, paramPageBean.getPageNo(), paramPageBean.getPageSize());
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            if(pageBean.getList().size()>0){
                List<Map<String,Object>> list = (List<Map<String, Object>>) pageBean.getList();
                for (int i = 0; i < pageBean.getList().size(); i++) {
                    Date classTime = (Date) list.get(i).get("classTime");
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    String classTimeStr=sdf.format(classTime);
                    if(stringObjectHashMap.get(classTimeStr) != null){
                        ArrayList<Map<String, Object>> listArrayList = (ArrayList<Map<String, Object>>) stringObjectHashMap.get(classTimeStr);
                        listArrayList.add(list.get(i));
                        stringObjectHashMap.put(classTimeStr,listArrayList);
                    }else {
                        Map<String, Object> map = list.get(i);
                        ArrayList<Map<String, Object>> lists = new ArrayList<>();
                        lists.add(list.get(i));
                        stringObjectHashMap.put(classTimeStr,lists);
                    }
                }
            }
            response.setData(stringObjectHashMap);
        } catch (Exception e) {
            resCode = 500;
            resMsg = "查看考勤列表失败!";
            e.printStackTrace();
        }
        //return new ApiResponse(resMsg, resCode).jsonResponse(false);
        return response.jsonResponse(true);
    }
    /**
     * 班级考勤详情
     * @param entity
     * @param token
     * @return
     */
    @Token(validateSign = true)
    @RequestMapping(value = "attendanceClassList", method = RequestMethod.POST)
    public String attendanceClassList(ViewMemberAttendClassRecord entity, PageBean paramPageBean, String token) {
        String resMsg = "查看考勤详情成功!";
        int resCode = 200;
        AppResponse response = new AppResponse();
        if(token == null && token.equals("")){
            resMsg = "token必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        try {
            Map<String , String []> unLockMap = getParameterMap(request);
            DetachedCriteria dc = DetachedCriteria.forClass(ViewMemberAttendClassRecord.class);
            HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
            //PageBean pageBean = tssMemberCheckAttendanceService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
            List<ViewMemberAttendClassRecord> byDetached = viewMemberAttendClassRecordService.findByDetached(dc);
            Map<String, Object> map = tssMemberCheckAttendanceService.attendanceStatistics(entity.getLessonsId());
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            if(byDetached.size()>0){
                ViewMemberAttendClassRecord viewMemberAttendClassRecord = byDetached.get(0);

                stringObjectHashMap.put("courseName",viewMemberAttendClassRecord.getCourseName());
                stringObjectHashMap.put("classTime",viewMemberAttendClassRecord.getMemberCheckAttendanceCreateTime());
                stringObjectHashMap.put("startTime",viewMemberAttendClassRecord.getMemberCheckAttendanceStartTime());
                stringObjectHashMap.put("type",viewMemberAttendClassRecord.getMemberCheckAttendanceType());
                stringObjectHashMap.put("sumShouldAttendAttendance",map.get("sumShouldAttendAttendance"));
                stringObjectHashMap.put("sumNotAttendance",map.get("sumNotAttendance"));
                stringObjectHashMap.put("sumRealAttendance",map.get("sumRealAttendance"));
                stringObjectHashMap.put("sumMakeUpAttendance",map.get("sumMakeUpAttendance"));
                stringObjectHashMap.put("list",byDetached);
            }else {
                resCode= 302;
                resMsg = "查看考勤详情失败！";
            }
            response.setData(stringObjectHashMap);
        } catch (Exception e) {
            resCode = 500;
            resMsg = "查看考勤详情失败!";
            e.printStackTrace();
        }
        //return new ApiResponse(resMsg, resCode).jsonResponse(false);
        return response.jsonResponse(true);
    }

    /**
     * 考勤
     *
     * @param entity
     *            被编辑对象
     * @return
     */
    /*@ResponseBody
    @RequestLog(content = "考勤" , save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "attendance" , method = RequestMethod.GET)*/
    @Token(validateSign = true)
    @RequestMapping(value = "attendance", method = RequestMethod.POST)
    public String attendance(TssMemberCheckAttendance entity,String adminUserId,String lessonsId, String token) {
        String resMsg = "考勤成功!";
        int resCode = 200;
        AppResponse response = new AppResponse();
        if(token == null && token.equals("")){
            resMsg = "token必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(adminUserId == null && "".equals(adminUserId)){
            resMsg = "用户信息必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(lessonsId == null && "".equals(lessonsId)){
            resMsg = "课节必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        try {
            String id=entity.getId();
            List<String> ids = new ArrayList<String>();
            List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
            List<TssMemberPackage> memberPackagehistorys = new ArrayList<TssMemberPackage>();
            if(StringUtils.isNotBlank(id)){
                ids.add(0,id);
                history = tssMemberCheckAttendanceService.findHistory(ids);
                memberPackagehistorys = tssMemberPackageService.findHistory(ids);
                int i = this.tssMemberCheckAttendanceService.attendance(entity.getAttendenceStatus(),adminUserId,entity.getId());
                //查询课时相关
                Map<String, Object> packageInformation = tssMemberCheckAttendanceService.findPackageInformation(id);
                Integer isComplimentaryMeals = Integer.parseInt(packageInformation.get("isComplimentaryMeals").toString());
                Integer formalClass = Integer.parseInt(packageInformation.get("formalClass").toString());
                boolean a = false;
                boolean b = false;
                boolean c = false;
                boolean d = false;
                TssMealFee tssMealFee = tssMealFeeService.findByStuId(packageInformation.get("memberId").toString());
                //修改课时 扣餐点费
                //考勤
                if(entity.getAttendenceStatus() == 1){
                    //历史考勤是未出勤
                    if(history.get(0).getAttendenceStatus() == 0){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.deductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
                        }else {
                            a = tssMemberPackageService.deductiveAttendanceDay(packageInformation);
                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),2);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,2,4,0,null);

                            //是否赠送餐点费 0不赠送 1赠送'   赠送餐点费则不扣除餐点费不产生权责
                            if(isComplimentaryMeals == 0){
                                //扣餐点费
                                b = tssMealFeeService.deductionDayMealFee(tssMealFee);

                                //餐点费权责
                                TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

                                packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
                                d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                            }if(isComplimentaryMeals == 1){
                                if(formalClass > 0){
                                    //扣餐点费
                                    b = tssMealFeeService.deductionDayMealFee(tssMealFee);

                                    //餐点费权责
                                    TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

                                    packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
                                    d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                                }
                            }

                            //查询餐点费是否  负费  进行提示
                            TssMealFee newTssMealFee = tssMealFeeService.findByStuId(packageInformation.get("memberId").toString());
                            if(newTssMealFee != null){
                                if(newTssMealFee.getSurplusAmout() < 0.00){
                                    //ajr.setResMsg("！餐点费余额不足，餐点费："+newTssMealFee.getSurplusAmout()+"");
                                    resMsg = "餐点费余额不足，餐点费"+newTssMealFee.getSurplusAmout();
                                }
                            }

                        }
                    }
                    //历史是缺勤
                    if(history.get(0).getAttendenceStatus() == 2){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.deductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
                        }else {
                            a = tssMemberPackageService.deductiveAttendanceDay(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),2);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,2,4,0,null);

                            //是否赠送餐点费 0不赠送 1赠送'   赠送餐点费则不扣除餐点费不产生权责
                            if(isComplimentaryMeals == 0){
                                //扣餐点费
                                b = tssMealFeeService.deductionDayMealFee(tssMealFee);

                                //餐点费权责
                                TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

                                packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
                                d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                            }if(isComplimentaryMeals == 1){
                                if(formalClass > 0){
                                    //扣餐点费
                                    b = tssMealFeeService.deductionDayMealFee(tssMealFee);

                                    //餐点费权责
                                    TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

                                    packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
                                    d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                                }
                            }
                        }
                    }
                    //历史是缺勤可补课
                    if(history.get(0).getAttendenceStatus() == 3){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.reductionMakeUpClassDeductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
                        }
                    }
                }
                //缺勤
                if(entity.getAttendenceStatus() == 2){
                    //历史是未考勤
                    if(history.get(0).getAttendenceStatus() == 0){
                        //维护缺勤天数
                        tssMemberPackageService.absenceRecord(packageInformation);
                    }
                    //历史是出勤
                    if(history.get(0).getAttendenceStatus() == 1){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.absenteeismDeductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,1,null);
                        }else {
                            a = tssMemberPackageService.absenteeismDeductiveAttendanceDay(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),2);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,4,1,null);

                            //是否赠送餐点费 0不赠送 1赠送'   赠送餐点费则不扣除餐点费不产生权责
                            if(isComplimentaryMeals == 0){
                                //加餐点费
                                b = tssMealFeeService.plusDayMealFee(tssMealFee);
                                //餐点费权责
                                TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

                                d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,1,tssMealFee.getDayMealFee());
                            }
                            if(isComplimentaryMeals == 1){
                                if(formalClass > 0){
                                    //加餐点费
                                    b = tssMealFeeService.plusDayMealFee(tssMealFee);
                                    //餐点费权责
                                    TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

                                    d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,1,tssMealFee.getDayMealFee());
                                }
                            }

                        }
                    }
                    //历史是缺勤可补课
                    if(history.get(0).getAttendenceStatus() == 3){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.reductionAbsenteeismDeductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,1,null);
                        }
                    }
                }
                //缺勤可补课
                if(entity.getAttendenceStatus() == 3){
                    //历史是未考勤
                    if(history.get(0).getAttendenceStatus() == 0){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.makeUpClassDeductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
                        }
                    }
                    //历史是出勤
                    if(history.get(0).getAttendenceStatus() == 1){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.addMakeUpClassDeductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
                        }
                    }
                    ////历史是缺勤
                    if(history.get(0).getAttendenceStatus() == 2){
                        if(entity.getType() == 1){
                            a = tssMemberPackageService.makeUpClassDeductiveHours(packageInformation);

                            //课时权责
                            TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

                            c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
                        }
                    }

                }
                if(!a){
                    //ajr.setResMsg("修改失败!");
                    resMsg = "考勤失败";
                }
                if(!c){
                    //ajr.setResMsg("修改失败!");
                    resMsg = "考勤失败";
                }
                if(entity.getType() == 2){
                    if(!b){
                        resMsg = "考勤失败";
                        //ajr.setResMsg("修改失败!");
                    }
                    if(!d){
                        resMsg = "考勤失败";
                        //ajr.setResMsg("修改失败!");
                    }
                }
            } else {
                resCode = 302;
                resMsg = "考勤失败";
            }
            if(entity.getCreateTime() == null){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
                entity.setCreateTime(formatter.parse(formatter.format(new Date())));
            }
            //更新班级考勤状态
            tssMemberCheckAttendanceService.updateClassAttendance(lessonsId);
            //添加操作记录
            if(history.size()>0){
                tssMemberCheckAttendanceHistoryService.batchSave(history);
            }
            //添加课时包记录
            if(memberPackagehistorys.size()>0){
                tssMemberPackageHistoryService.batchSave(memberPackagehistorys);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //ajr.setResCode(500);
            //ajr.setResMsg("修改失败(" + e.getMessage() + ")!");
            resCode = 500;
            resMsg = "考勤失败";
        }
        //return ajr.jsonResponse(false);
        return new AppResponse(resMsg, resCode).jsonResponse(false);
    }
    @Token(validateSign = true)
    @RequestMapping(value = "batchAttendance", method = RequestMethod.POST)
    public String batchAttendance(TssMemberCheckAttendance entity,String idsStr,String lessonsId,String adminUserId,String token) {
        String[] split = idsStr.split(",");
        List<String> ids = Arrays.asList(split);
        /*List<String> ids = new ArrayList<String>();
        for (int i = 0; i <split.length ; i++) {
            ids.add((String)ids[i]);
        }*/
        String resMsg = "考勤成功!";
        int resCode = 200;
        AppResponse response = new AppResponse();
        if(token == null && token.equals("")){
            resMsg = "token必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(adminUserId == null && "".equals(adminUserId)){
            resMsg = "用户信息必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(lessonsId == null && "".equals(lessonsId)){
            resMsg = "课节必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        if(idsStr == null && idsStr.equals("")){
            resMsg = "学生必传";
            return new AppResponse(resMsg, 301).jsonResponse(false);
        }
        try {
            String[] str = request.getParameterValues("ids");
            List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
            List<TssMemberPackage> memberPackagehistorys = new ArrayList<TssMemberPackage>();
            if(ids.size() > 0){
                history = tssMemberCheckAttendanceService.findHistory(ids);
                memberPackagehistorys = tssMemberPackageService.findHistory(ids);

                int i = tssMemberCheckAttendanceService.batchAttendance(ids,adminUserId);

                //查询课时包相关信息
                List<Map<String, Object>> packageInformation = tssMemberCheckAttendanceService.findPackageInformationList(ids);
                //批量扣除课时
                boolean c = false;
                boolean d = false;
                if(entity.getType() == 1){
                    boolean a = tssMemberPackageService.batchDeductiveHours(packageInformation);
                    //批量产生课时权责
                    for (Map<String,Object> map : packageInformation) {
                        map.get("id");
                        TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),1);

                        c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,1,1,0,null);
                        if(!c){
                            resMsg = "考勤失败";
                        }
                    }
                }else {
                    boolean a = tssMemberPackageService.batcheductiveAttendanceDay(packageInformation);

                    //扣餐点费  赠送天数不计数的不扣除餐点费
                    boolean b = tssMealFeeService.batchDeductionDayMealFee(packageInformation);


                    //批量产生课时权责
                    for (Map<String,Object> map : packageInformation) {
                        //课时权责
                        TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),2);

                        c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,2,4,0,null);

                        Integer isComplimentaryMeals = Integer.parseInt(map.get("isComplimentaryMeals").toString());
                        Integer formalClass = Integer.parseInt(map.get("formalClass").toString());
                        if(isComplimentaryMeals == 0){
                            //餐点费权责
                            TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

                            TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
                            map.put("dayMealFee",tssMealFee.getDayMealFee());
                            d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                        }
                        if(isComplimentaryMeals == 1){
                            if(formalClass > 0){
                                //餐点费权责
                                TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

                                TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
                                map.put("dayMealFee",tssMealFee.getDayMealFee());
                                d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                            }
                        }
                    }
                }
                //更新班级考勤状态
				/*if(entity.getCreateTime() == null){
					entity.setCreateTime(new Date());
				}*/
                tssMemberCheckAttendanceService.updateClassAttendance(lessonsId);
                //添加操作记录
                if(history.size()>0){
                    tssMemberCheckAttendanceHistoryService.batchSave(history);
                }
                //添加课时包记录
                if(memberPackagehistorys.size()>0){
                    tssMemberPackageHistoryService.batchSave(memberPackagehistorys);
                }
            } else {
                resCode = 302;
                resMsg = "考勤失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            resCode = 500;
            resMsg = "考勤失败";
        }
        //return ajr.jsonResponse(false);
        return new AppResponse(resMsg, resCode).jsonResponse(false);
    }
    /**
     * 批量考勤
     *
     * @param entity
     *            被编辑对象
     * @return
     */
/*    @ResponseBody
    @RequestLog(content = "批量考勤" , save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "batchAttendance" , method = RequestMethod.GET)*/
    /*@Token(validateSign = true)
    @RequestMapping(value = "batchAttendance", method = RequestMethod.POST)
    public String batchAttendance(TssMemberCheckAttendance entity,@RequestParam("ids[]") List<String> ids,String lessonsId,String adminUserId,String token) {
        String resMsg = "考勤成功!";
        int resCode = 200;
        ApiResponse response = new ApiResponse();
        if(token == null && token.equals("")){
            resMsg = "token必传";
            return new ApiResponse(resMsg, 301).jsonResponse(false);
        }
        if(adminUserId == null && "".equals(adminUserId)){
            resMsg = "用户信息必传";
            return new ApiResponse(resMsg, 301).jsonResponse(false);
        }
        if(lessonsId == null && "".equals(lessonsId)){
            resMsg = "课节必传";
            return new ApiResponse(resMsg, 301).jsonResponse(false);
        }
        if(ids.size() > 0){
            resMsg = "学生必传";
            return new ApiResponse(resMsg, 301).jsonResponse(false);
        }
        try {
            String[] str = request.getParameterValues("ids");
            List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
            List<TssMemberPackage> memberPackagehistorys = new ArrayList<TssMemberPackage>();
            if(ids.size() > 0){
                history = tssMemberCheckAttendanceService.findHistory(ids);
                memberPackagehistorys = tssMemberPackageService.findHistory(ids);

                int i = tssMemberCheckAttendanceService.batchAttendance(ids,adminUserId);

                //查询课时包相关信息
                List<Map<String, Object>> packageInformation = tssMemberCheckAttendanceService.findPackageInformationList(ids);
                //批量扣除课时
                boolean c = false;
                boolean d = false;
                if(entity.getType() == 1){
                    boolean a = tssMemberPackageService.batchDeductiveHours(packageInformation);
                    //批量产生课时权责
                    for (Map<String,Object> map : packageInformation) {
                        map.get("id");
                        TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),1);

                        c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,1,1,0,null);
                        if(!c){
                            resMsg = "考勤失败";
                        }
                    }
                }else {
                    boolean a = tssMemberPackageService.batcheductiveAttendanceDay(packageInformation);

                    //扣餐点费  赠送天数不计数的不扣除餐点费
                    boolean b = tssMealFeeService.batchDeductionDayMealFee(packageInformation);


                    //批量产生课时权责
                    for (Map<String,Object> map : packageInformation) {
                        //课时权责
                        TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),2);

                        c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,2,4,0,null);

                        Integer isComplimentaryMeals = Integer.parseInt(map.get("isComplimentaryMeals").toString());
                        Integer formalClass = Integer.parseInt(map.get("formalClass").toString());
                        if(isComplimentaryMeals == 0){
                            //餐点费权责
                            TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

                            TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
                            map.put("dayMealFee",tssMealFee.getDayMealFee());
                            d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                        }
                        if(isComplimentaryMeals == 1){
                            if(formalClass > 0){
                                //餐点费权责
                                TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

                                TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
                                map.put("dayMealFee",tssMealFee.getDayMealFee());
                                d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
                            }
                        }
                    }
                }
                //更新班级考勤状态
				*//*if(entity.getCreateTime() == null){
					entity.setCreateTime(new Date());
				}*//*
                tssMemberCheckAttendanceService.updateClassAttendance(lessonsId);
                //添加操作记录
                if(history.size()>0){
                    tssMemberCheckAttendanceHistoryService.batchSave(history);
                }
                //添加课时包记录
                if(memberPackagehistorys.size()>0){
                    tssMemberPackageHistoryService.batchSave(memberPackagehistorys);
                }
            } else {
                resCode = 302;
                resMsg = "考勤失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            resCode = 500;
            resMsg = "考勤失败";
        }
        //return ajr.jsonResponse(false);
        return new ApiResponse(resMsg, resCode).jsonResponse(false);
    }*/
}